package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.StaffInstitutionEntity;
import com.tfjybj.itoo.teach.model.StaffInstitutionModel;
import com.tfjybj.itoo.teach.provider.service.StaffInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * StaffInstitutionController
 * staffInstitution表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"staffInstitution表接口"})
@RequestMapping(value = "/staffInstitution")
@RestController
@Slf4j
public class StaffInstitutionController {

    @Resource
    private StaffInstitutionService staffInstitutionService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StaffInstitutionModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StaffInstitutionModel model) {
		if (StringUtils.isEmpty(model.getStaffId())){
            return ItooResult.build(ItooResult.FAIL, "staffId为空");
        }
		if (StringUtils.isEmpty(model.getInstitutionId())){
            return ItooResult.build(ItooResult.FAIL, "institutionId为空");
        }
        StaffInstitutionEntity staffInstitutionEntity = new StaffInstitutionEntity();
        BeanUtils.copyProperties(model, staffInstitutionEntity);
        staffInstitutionService.save(staffInstitutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        staffInstitutionService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        staffInstitutionService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StaffInstitutionModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改staffInstitution")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StaffInstitutionModel model) {
		if (StringUtils.isEmpty(model.getStaffId())){
            return ItooResult.build(ItooResult.FAIL, "staffId为空");
        }
		if (StringUtils.isEmpty(model.getInstitutionId())){
            return ItooResult.build(ItooResult.FAIL, "institutionId为空");
        }
        StaffInstitutionEntity staffInstitutionEntity = new StaffInstitutionEntity();
        BeanUtils.copyProperties(model, staffInstitutionEntity);
        staffInstitutionService.updateById(staffInstitutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找StaffInstitution
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        StaffInstitutionEntity staffInstitutionEntity = staffInstitutionService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutionEntity);
    }

    /**
     * 分页查询所有StaffInstitution
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有StaffInstitution")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<StaffInstitutionEntity> staffInstitutions = staffInstitutionService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutions);
    }
	
	/**
     * 根据职工id模糊查询staffInstitution
     *
     * @param staffId  职工id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的staffInstitution
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据职工id模糊查询staffInstitution", notes = "分页根据职工id模糊查询staffInstitution")
    @GetMapping(value = "queryStaffInstitutionByLikeStaffId/{pageNo}/{pageSize}")
    public ItooResult queryStaffInstitutionByLikeStaffId(@RequestParam(required = false, defaultValue = "") String staffId,
                                                         @ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                         @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffInstitutionEntity> staffInstitutionList = staffInstitutionService.queryByLikeStaffId(staffId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutionList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 更新教师主要机构
     *
     * @param staffId 教师编码
     * @param academyId 机构ID
     * @return 更新成功或失败
     * @author 张家豪
     * @since 2.0.0 2018年11月23日23:09:21
     */
    @ApiOperation(value = "更新教师主要机构")
    @PostMapping(value = {"/updateByStaffIdAndIsmain/{staffId}/{academyId}"})
    public ItooResult updateByStaffIdAndIsmain(@PathVariable String staffId, @PathVariable String academyId) {
        boolean result;
        try {
            result = staffInstitutionService.updateByStaffIdAndIsmain(staffId,academyId);
            if (result){
                return ItooResult.build(ItooResult.SUCCESS, "修改成功");
            }
        } catch (Exception e) {
            log.error("control",e);
            return ItooResult.build("1111","失败",e);
        }
        return ItooResult.build(ItooResult.FAIL, "修改失败");
    }

    /**
     * 删除该教师的所有附属机构
     *
     * @param staffId 教师编码
     * @return 更新成功或失败
     * @author 张家豪
     * @since 2018年11月24日22:55:01
     */
    @ApiOperation(value = "删除该教师的所有附属机构")
    @PostMapping(value = {"/deleteSubAcademy/{staffId}"})
    public ItooResult deleteSubAcademy(@PathVariable String staffId) {
        boolean result;
        try {
            result = staffInstitutionService.deleteSubAcademy(staffId);
            if (result){
                return ItooResult.build(ItooResult.SUCCESS, "删除成功");
            }else {
                return ItooResult.build(ItooResult.SUCCESS, "没有可删除的");
            }
        } catch (Exception e) {
            log.error("control",e);
            return ItooResult.build("1111","失败",e);
        }

    }


}    
